<?php
/**
 * @package Joomla
 * @subpackage Jmonoslideshow
 * @copyright (C) 2010 - Matthieu BARBE - www.ccomca.com
 * @license GNU/GPL v2
 * 
 * Jmonoslideshow is a Joomla extension for Monoslideshow.
 *
 * Jmonoslideshow uses :
 * Monoslideshow (http://www.monoslideshow.com/)
 * Plupload (http://www.plupload.com/) by Moxicode Systems
 * json class by Michal Migurski, Matt Knapp, Brett Stimmerman
 * jquery a JavaScript Library (http://jquery.com/)
 * getid3 library (http://getid3.sourceforge.net/)
 * swfobject is an easy-to-use and standards-friendly method to embed Flash content, which utilizes one small JavaScript file (http://code.google.com/p/swfobject/)
 * Chained Selects jQuery Plugin (http://www.appelsiini.net/2010/jquery-chained-selects)
 *
 * Jmonoslideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.model');

/**
 * @package		Joomla
 * @subpackage	Jmonoslideshow
 */
class JmonoslideshowModelAlbums extends JModel
{
	/**
	 * Album data array
	 *
	 * @var array
	 */
	var $_dataAlb = null;
	
	/**
	 * Image data array
	 *
	 * @var array
	 */
	var $_dataImage = null;
	
	
	/**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
	function _buildAlbumsWhere()
	{		
		$where = " ";
		global $mainframe;
		$params 	= &$mainframe->getParams();
		$category = $params->get('singlecategory');
		//filter media type
		$typemedia = $params->get('typemedia', "all");
		if ($typemedia != "all")
		{ 
    		if( is_array( $typemedia ) ) {
        	$typeCondition = ' AND (m.type IN ( ' . implode( ',', $typemedia ) . ') )';
	    	} else {
			$typeCondition = ' AND (m.type = '.$typemedia.')';
			}	
		}
		$wheremedia = "";
		if ($typemedia != "all") $wheremedia .= $typeCondition;
		$where = ' WHERE a.published = 1 AND a.catid = '.$category.''.$wheremedia;
		return $where;
	}
	
	
	
	function _buildAlbumsOrderBy()
	{
		
		global $mainframe;
		$params 	= &$mainframe->getParams();
		$order = $params->get('order_album', 'ordering');
		$sort = $params->get('sort_album', 'asc');
		$orderby_cat = ' ORDER BY a.'.$order.' '.$sort;
		
		return $orderby_cat;
	}
	
	
	function _builAlbumsdQuery()
	{
		// Get the WHERE and ORDER BY clauses for the query
		$where 					= $this->_buildAlbumsWhere();
		$orderby 				= $this->_buildAlbumsOrderBy();
		
		$query = ' SELECT a.id, a.title, a.description, COUNT( m.id ) AS numitems '
			. ' FROM #__monoslideshow_album  AS a '
			. ' LEFT JOIN #__monoslideshow AS m ON m.albumid  = a.id'
			. 	$where
			. ' GROUP BY a.id'
			. ' HAVING (numitems >0)'
			.   $orderby;
		;
		
		return $query;
	}
	
	function _loadAlbums()
	{
		// Load the Album data
		// Lets load the data if it doesn't already exist
		if (empty($this->_dataAlb))
		{
			$query = $this->_builAlbumsdQuery();
			$this->_dataAlb = $this->_getList( $query );
		}
		return true;
	}
	
	/**
	 * Retrieves the album  data
	 * @return array Array of objects containing the data from the database
	 */
	function getAlbums()
	{
		// Load the Category data
		// Lets load the data if it doesn't already exist
		$this->_loadAlbums();
		return $this->_dataAlb;
	}
	
	/**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
	function _buildImagesWhere()
	{		
		
		global $mainframe;
		$params 	= &$mainframe->getParams();
		$category = $params->get('singlecategory');
		//filter media type
		$typemedia = $params->get('typemedia', "all");
		if ($typemedia != "all")
		{ 
    		if( is_array( $typemedia ) ) {
        	$typeCondition = ' AND (m.type IN ( ' . implode( ',', $typemedia ) . ') )';
	    	} else {
			$typeCondition = ' AND (m.type = '.$typemedia.')';
			}	
		}
		$wheremedia = "";
		if ($typemedia != "all") $wheremedia .= $typeCondition;
		$where = ' WHERE a.published = 1 AND m.published = 1 AND a.catid = '.$category.''.$wheremedia;
		//$where = ' WHERE a.catid = 7';
		return $where;
	}
	
	
	
	function _buildImagesOrderBy()
	{
		global $mainframe;
		$params 	= &$mainframe->getParams();
		$order = $params->get('order_media', 'ordering');
		$sort = $params->get('sort_media', 'asc');
		$orderby_cat = ' ORDER BY a.'.$order.' '.$sort;
	}
	 
	function _buildImagesQuery()
	{			
		// Get the WHERE and ORDER BY clauses for the query
		$where		= $this->_buildImagesWhere();
		$orderby 	= $this->_buildImagesOrderBy();
		
		
		global $mainframe;
		
		
		$query = ' SELECT m.* '
			.  ' FROM #__monoslideshow AS m  '
			.  ' LEFT JOIN #__monoslideshow_album AS a  '
			.  '  ON m.albumid = a.id '
			. 	$where
			. 	$orderby;
		;		

		return $query;
	}
	
	function _loadImages()
	{
		// Load the Images data
		// Lets load the data if it doesn't already exist
		if (empty($this->_dataImage))
		{
			$query = $this->_buildImagesQuery();
			$this->_dataImage = $this->_getList( $query );
		}
		return true;
	}	
	
	/**
	 * Retrieves the images data
	 * @return array Array of objects containing the data from the database
	 */
	function getImages()
	{
		// Load the Image data
		// Lets load the data if it doesn't already exist
		$this->_loadImages();
		return $this->_dataImage;
	}
}